﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Transactions;
#if !NET40
using CoNatural.Helpers;
#endif

namespace CoNatural.Data {
	public static class Extensions {
		#region Batch Scripting

		static public string ScriptBatch(this IConnection connection, IEnumerable<ICommand> commands) {
			using (var bctx = new BatchExecutionContext(connection, "Batch", false)) {
				foreach (var command in commands) {
					var ctx = new CommandExecutionContext(connection, command);
					bctx.AddCommand(ctx);
				}
				return connection.DataBinder.DbProvider.ScriptBatch(bctx);
			}
		}

		static public string ScriptBatch(this IConnection connection, ICommand iCommand, IEnumerable<object> objs) {
			using (var bctx = new BatchExecutionContext(connection, "Batch", true)) {
				foreach (var obj in objs) {
					var ctx = new CommandExecutionContext(connection, iCommand, obj);
					bctx.AddCommand(ctx);
				}
				return connection.DataBinder.DbProvider.ScriptBatch(bctx);
			}
		}

		static public string ScriptBatch<T>(this IConnection connection, IEnumerable<T> objs, Func<T, Tuple<object, ICommand>> tupleBinder) {
			using (var bctx = new BatchExecutionContext(connection, "Batch", false)) {
				bctx.AddCommands<T>(objs, tupleBinder);
				return connection.DataBinder.DbProvider.ScriptBatch(bctx);
			}
		}

		#endregion Batch Scripting
	}
}
